package vip.dulaogou.park;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author  duyouwei
 * @create  2021/4/19 13:19
 * @description
 * 发票类
 * 当车子驶出时生成一张发票
 **/
public class Invoice {
    private static Integer count = 1;   /*用来获取发票id的，每次调用构造函数，count+1*/
    private Integer invoiceId;   /*发票id*/
    private String carId;  /*车子的id*/
    private Integer parkLocation;   /*车子的停车位置*/
    private Long startTime; /*开始时间*/
    private Long endTime;   /*结束时间*/
    private Long intervalTime;  /*间隔时间*/
    private Integer cost;   /*费用*/

    public Invoice() {
        invoiceId = count++;
    }

    public Invoice(String carId, Integer parkLocation, Long startTime, Long endTime) {
        invoiceId = count++;
        this.carId = carId;
        this.parkLocation = parkLocation;
        this.startTime = startTime;
        this.endTime = endTime;

        /*自动生成时间间隔和消费金额*/
        // setIntervalTime();
        // setCost();
    }

    public Integer getInvoiceId() {
        return invoiceId;
    }

    public String getCarId() {
        return carId;
    }

    public Integer getParkLocation() {
        return parkLocation;
    }

    public Long getStartTime() {
        return startTime;
    }

    public Long getEndTime() {
        return endTime;
    }

    public Long getIntervalTime() {
        return intervalTime;
    }

    public Integer getCost() {
        return cost;
    }

    public void setCarId(String carId) {
        this.carId = carId;
    }

    public void setParkLocation(Integer parkLocation) {
        this.parkLocation = parkLocation;
    }

    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }

    public void setEndTime(Long endTime) {
        this.endTime = endTime;
    }

    public void setIntervalTime() {
        if(null != endTime && null != startTime) {
            intervalTime = endTime - startTime;
        }
    }

    /*
    * [0-30]分钟不收费
    * (30-60]分钟10块钱
    * (60-120]分钟20块钱
    * 以此类推
    * */
    public void setCost() {
        if(null != intervalTime) {
            /*时间向上取整*/
            int intervalMins = (int) Math.ceil(intervalTime / 1000.0 / 60.0);
            if (intervalMins <= 30) {
                cost = 0;
            } else {
                // extra作用是判断当前分钟数是不是60的整数倍
                // 由于分钟是向上取整的，所以如果正好是60的整数倍，说明还没超过1小时，
                // 不应该额外收费，extra = 0
                // 否则应该计入下一个收费小时，extra = 1
                int extra = intervalMins % 60 == 0 ? 0 : 1;
                cost = (intervalMins / 60 + extra) * 10;
            }
        }
    }

    @Override
    public String toString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");

        /*把间隔时间转化为小时-分钟-秒*/
        int hour = 0, minute = 0, second = 0;
        if(null != endTime){
            hour = (int) (intervalTime / 1000 / 60 / 60);
            minute = (int) (intervalTime / 1000 / 60 - (hour * 60));
            second = (int) Math.ceil(intervalTime / 1000.0 - (hour * 60.0 * 60.0) - (minute * 60.0));
        }
        return "Invoice{" +
                "发票编号='" + invoiceId + '\'' +
                ", 车牌号='" + carId + '\'' +
                ", 停车位=" + parkLocation +
                ", 开始时间=" + sdf.format(new Date(startTime)) +
                ", 结束时间=" + sdf.format(new Date(endTime)) +
                ", 停车时长=" + (hour + "时" + minute + "分" + second + "秒") +
                ", 总消费金额=" + cost + "元" +
                '}';
    }

    /*由于发票id是唯一的，我们只需要保证id相同就能保证发票是相同的*/
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Invoice record = (Invoice) o;
        return Objects.equals(invoiceId, record.invoiceId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(invoiceId);
    }
}
